<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/29
 * Time: 17:31
 */

namespace app\models;

use yii;
use yii\db\Query;
use app\lib\Redis;
/**
 *客户来源表
 *
 */
class CustomerRefer
{
    use Redis;

    private $redis;

    private $table;

    public function __construct()
    {
        $this->table = yii::$app->db->tablePrefix . 'customer_refer';
        $this->redis = $this->getInstance();
    }

    /**客户来源list
     * @return array
     */
    public function list()
    {
        $redisKey = 'customer:refer';
        if ($list = $this->redis->get($redisKey)) {
            return json_decode($list, true);
        } else {
            $db = new Query();
            $list = $db->select('*')->from($this->table)->all();
            $this->redis->setex($redisKey, 3600, json_encode($list));
            return $list;
        }
    }

    public function add($data)
    {
        yii::$app->db->createCommand()->insert($this->table, $data)->execute();
        $this->redis->delete('customer:refer');
        return true;
    }
}